home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / asm / games / texturemapping / polyfill.asm < prev    next >
Assembly Source File  |  1980-01-03  |  5KB  |  289 lines

  1. ;$$TABS=8
  2. ;
  3. ; PolyFill.asm - simple, stupid polygon filler. No heavy attempt at
  4. ;     optimization, since this demo doesn't really use it much.
  5.  
  6.     include    'demo.i'
  7.  
  8. VERTEX_SIZE    equ    4*2    ; xyuv
  9.  
  10.     xref    YLRFill,YLRCCFill,YLRUUFill
  11.  
  12. fracbits    equ    6
  13.  
  14. get_right:
  15. ; now, loop until y change
  16.     move.l    d5,save_d5(a6)
  17.     move.l    right_ptr(a6),a2
  18. gr_1:    move    (a2),d4
  19.     move.w    4(a2),d5    ; rightc
  20.     lea    VERTEX_SIZE(a2),a2
  21.     cmp.l    a0,a2
  22.     bne.s    gr_2
  23.     lea    vertex(a6),a2
  24. gr_2    move    (a2),d1
  25.     move    2(a2),d7
  26.     subq    #1,d0        ; better check for neg
  27.     ble.s    got_right
  28.     cmp    d7,d2        ; same y ?
  29.     ble.s    gr_1
  30. ; now, d4,d2=top d1/d7=bot
  31.     move.w    d5,c_right(a6)
  32.     sub    4(a2),d5
  33.     ext.l    d5
  34.     move    d7,rbot(a6)
  35.     sub    d2,d7
  36.     divs    d7,d5
  37.     move.w    d5,dc_right(a6)
  38.     neg    d7
  39.     sub    d4,d1        ; dx
  40.     ext.l    d1
  41.     lsl.l    #fracbits,d1
  42.     divs    d7,d1        ; dx/dy<<fracbits
  43.     ext.l    d1
  44.     moveq    #16-fracbits,d7
  45.     lsl.l    d7,d1
  46.     swap    d4
  47.     clr    d4
  48.     move.l    save_d5(a6),d5
  49. got_right:
  50.     move.l    a2,right_ptr(a6)
  51.     rts
  52.  
  53. get_left:
  54.     move.l    left_ptr(a6),a1
  55.     move.l    d5,save_d5(a6)
  56.     move.l    d4,save_d4(a6)
  57. ; now, loop until y change
  58. gl_1:    move    (a1),d3
  59.     move    4(a1),d4
  60.     lea    -VERTEX_SIZE(a1),a1
  61.     cmp.l    #vertex-VERTEX_SIZE,a1
  62.     bne.s    gl_2
  63.     lea    -VERTEX_SIZE(a0),a1
  64. gl_2    move    (a1),d5
  65.     move    2(a1),d7
  66. ; same y
  67.     subq    #1,d0        ; better check for neg
  68.     ble.s    got_left_lst
  69.     cmp    d7,d2        ; same y ?
  70.     ble.s    gl_1
  71. ; now, d3,d2=top d1/d7=bot
  72.     move.w    d4,c_left(a6)
  73.     move    d7,lbot(a6)
  74.     sub    d2,d7
  75.     sub    4(a1),d4
  76.     ext.l    d4
  77.     divs    d7,d4
  78.     move.w    d4,dc_left(a6)
  79.     neg    d7
  80.     sub    d3,d5        ; dx
  81.     ext.l    d5
  82.     lsl.l    #fracbits,d5
  83.     divs    d7,d5        ; dx/dy<<fracbits
  84.     ext.l    d5
  85.     moveq    #16-fracbits,d7
  86.     lsl.l    d7,d5
  87.     swap    d3
  88.     clr    d3
  89.     move.l    a1,left_ptr(a6)
  90.     move.l    save_d4(a6),d4
  91. got_left
  92.     rts
  93. got_left_lst
  94.     move.l    a1,left_ptr(a6)
  95.     move.l    save_d5(a6),d5
  96.     rts
  97.  
  98. UnClippedPolygon::
  99. ; UnClippedPolygon - draw an unclipped polygon
  100. ; entr: VERTEX=x,y x,y x,y ...
  101. ; D0=nverts (3 for a triangle)
  102. ; except for termination, this routine fits entirely in the 256 byte cache.
  103.  
  104.     ONTIMER    5
  105.     move    d0,d1        ; loop ctr
  106.  
  107.     subq    #1,d1
  108.     moveq    #-127,d2        ; highest so far
  109.     lea    vertex(a6),a0
  110.     move.l    a0,a3        ; leftmost vertex
  111.     move.l    a0,a4        ; rightmost vertex
  112. up_1:    move.w    (a0),d3
  113.     cmp.w    (a3),d3
  114.     bge.s    no_new_left
  115.     move.l    a0,a3
  116. no_new_left:
  117.     cmp.w    (a4),d3
  118.     ble.s    no_new_right
  119.     move.l    a0,a4
  120. no_new_right:
  121.     cmp    2(a0),d2
  122.     bgt.s    up_2
  123.     move    2(a0),d2
  124.     move.l    a0,a1        ; pointer to top
  125. up_2:    lea    VERTEX_SIZE(a0),a0
  126.     dbra    d1,up_1
  127.     move.l    a1,a2
  128.     movem.l    a1/a2/a3/a4,left_ptr(a6)
  129.  
  130. ; now, fill in vtable!
  131.     move.l    YLRFiller(a6),d1
  132.     cmp.l    #YLRUUFill,d1
  133.     bne.s    done_vtable
  134.     move.w    (a3),d1        ; leftx
  135.     move.w    6(a3),d5    ; leftv
  136.     move.w    (a4),d3        ; rightx
  137.     move.w    6(a4),d4    ; rightv
  138.     move.l    current_tmap(a6),a4
  139.     move.w    4(a4),d6    ; shift up
  140.     lea    6(a4),a4
  141.     lea    (vtable.w,a6,d1.w*4),a2    ; outv
  142.     sub    d5,d4        ; rv-lv
  143.     addq    #1,d4
  144.     sub    d1,d3        ; rx-lx
  145.     addq    #1,d3
  146.     ext.l    d4
  147.     divs    d3,d4
  148.     subq    #1,d3
  149. 1$:    moveq    #0,d7
  150.     move.w    d5,d7
  151.     lsr.w    #8,d7
  152.     lsl.l    d6,d7
  153.     add.l    a4,d7
  154.     move.l    d7,(a2)+
  155.     add.w    d4,d5
  156.     dbra    d3,1$
  157. done_vtable:
  158.     addq    #1,d0
  159.     bsr    get_left
  160.     bsr    get_right
  161.  
  162. ; register usage:
  163. ; d0=#verts left
  164. ; d1=rightdx<<16
  165. ; d2=cury
  166. ; d3=leftx<<16
  167. ; d4=rightx<<16
  168. ; d5=leftdx<<16
  169. ; d6=ycounter
  170.  
  171. ; (a0)=end of vertex buffer+4
  172. ; (a3)=ylr ptr
  173.     lea    temp_ylr(pc),a3
  174. new_trapezoid:
  175.     tst    d0
  176.     ble.s    do_end
  177.     move    d2,d6        ; top y
  178.     move    d2,d7
  179.     sub    lbot(a6),d6
  180.     sub    rbot(a6),d7        
  181.     cmp    d6,d7
  182.     bge.s    no_r_ends_first
  183.     move    d7,d6
  184. no_r_ends_first:
  185.     move.w    d0,-(a7)
  186.     move.w    c_left(a6),d7
  187.     move.w    c_right(a6),d0
  188.     move.w    dc_left(a6),a1
  189.     move.w    dc_right(a6),a2
  190.     ONTIMER    11
  191.  
  192.     subq    #1,d6
  193. fill_lp:
  194.     move    d2,(a3)+
  195.     swap    d3
  196.     subq    #1,d2
  197.     move    d3,(a3)+
  198.     swap    d3
  199.     swap    d4
  200.     move    d4,(a3)+
  201.     swap    d4
  202.     add.l    d1,d4
  203.     move.w    d7,(a3)+
  204.     add.w    a1,d7
  205.     move.w    d0,(a3)+
  206.     add.w    a2,d0
  207.     add.l    d5,d3
  208.     dbra    d6,fill_lp
  209.     OFFTIMER    11
  210.     move.w    d0,c_right(a6)
  211.     move.w    d7,c_left(a6)
  212.     move.w    (a7)+,d0
  213.     cmp    lbot(a6),d2
  214.     bne.s    no_l_chg
  215.     bsr    get_left
  216. no_l_chg:
  217.     cmp    rbot(a6),d2
  218.     bne    new_trapezoid
  219.     bsr    get_right
  220.     bra    new_trapezoid
  221.  
  222. do_end:
  223. ; do last scan line
  224.     lea    temp_ylr(pc),a4
  225.     cmp.l    a3,a4
  226.     bne.s    tr_1
  227. ; no scan-lines were output, so fill a horizontal line between max and min x/y
  228. yes_fill1:
  229.     move.l    leftmost_vertex(a6),a0
  230.     move.l    rightmost_vertex(a6),a1
  231.     move    d2,(a3)+
  232.     move    (a0),(a3)+
  233.     move    (a1),(a3)+
  234.     move    4(a0),(a3)+
  235.     move    4(a1),(a3)+
  236.     st    (a3)
  237.     lea    temp_ylr(pc),a0
  238.     OFFTIMER    5
  239.     move.l    YLRFiller(a6),a1
  240.     jmp    (a1)
  241. tr_1:    move    d2,(a3)+
  242.     swap    d3
  243.     swap    d4
  244.     move    d3,(a3)+
  245.     move    d4,(a3)+
  246.     move.l    c_left(a6),(a3)+
  247. no_fill2:
  248.     st    (a3)
  249.     lea    temp_ylr(pc),a0
  250.     OFFTIMER    5
  251.     move.l    YLRFiller(a6),a1
  252.     jmp    (a1)
  253.  
  254. temp_ylr::
  255.     ds.w    (DUNGEON_WINDOW_HEIGHT+3)*5+1
  256.  
  257.  
  258.     section    __MERGED,data
  259. YLRFiller::    dc.l    YLRCCFill
  260. save_d5::    ds.l    1
  261. save_d4::    ds.l    1
  262. rbot::    ds.w    1
  263. lbot::    ds.w    1
  264. ; tmapped polygon data fmt:
  265. ; xyuv
  266. vertex::
  267.     ds.b    50*VERTEX_SIZE    ; enough room for 50 point poly
  268. left_ptr::    dc.l    0
  269. right_ptr::    dc.l    0
  270. leftmost_vertex::
  271.     dc.l    0
  272. rightmost_vertex::
  273.     dc.l    0
  274.  
  275. c_left::    dc.w    $2000
  276. c_right::    dc.w    $4000
  277. dc_left::    dc.w    $0000
  278. dc_right::    dc.w    $0000
  279. current_tmap::    dc.l    0
  280.  
  281.     dc.l    0
  282. vtable::    ds.l    DUNGEON_WINDOW_WIDTH
  283.     dc.l    0
  284.  
  285.  
  286.     end
  287.  
  288.